package com.mzx.controller;

import com.mzx.api.LocalDateService;
import com.mzx.pojo.Product;
import com.mzx.service.ProductService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class IndexController {

    @Autowired
    ProductService productService;

    @Reference
    private LocalDateService localDateService;


    @RequestMapping("/list")
    @CrossOrigin(origins = "127.0.0.1")
    @ResponseBody
    public Map<String,Object> findAll(){
        List<Product> list = productService.queryList();
        Map<String,Object> resutmap = new HashMap<String,Object>();
        resutmap.put("code",200);
        resutmap.put("data",list);
        return resutmap;
    }

    @RequestMapping("/time")
    @ResponseBody
    public String time(){
        // id=123
        String localtime = localDateService.getLocalDateTimeStr();
        System.out.println(localtime);
        return localtime;
    }

    @RequestMapping("/res")
    @ResponseBody
    public String res(HttpServletRequest request, HttpServletResponse response){
        // id=123
        String id = request.getParameter("id");
        // values=a&values=b
        String[] parameterValues = request.getParameterValues("values");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Enumeration<String> parameterNames = request.getParameterNames();
        Collections.list(parameterNames).stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
        Object name = request.getAttribute("name");
        Enumeration<String> attributeNames = request.getAttributeNames();
        System.out.println(id+"===>"+parameterNames.toString()+"===>"+name);
        System.out.println("param-request");
        return "res";
    }
}
